﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using AppLayer;
using SpotItComponents;
using SpotItServerAPI;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;

namespace SpotIt
{
    /// <summary>
    /// Interaction logic for SpotIt_SetupDeckWindow.xaml
    /// </summary>
    public partial class SpotIt_SetupDeckWindow : Window
    {
        private Services services;
        private Session session;
        private Deck deck;
        private SymbolSet symbolSet;

        public SpotIt_SetupDeckWindow()
        {
            InitializeComponent();
        }

        public SpotIt_SetupDeckWindow(Services services, Session session, Deck deck, SymbolSet symbolSet)
        {
            InitializeComponent();

            this.services = services;
            this.session = session;
            this.deck = deck;
            this.symbolSet = symbolSet;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            deckLabelTextBox.Text = deck.Label;
            if (deck.BackImage != null)
            {
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                MemoryStream ms = new MemoryStream();

                // Save to a memory stream...
                deck.BackImage.Save(ms, ImageFormat.Bmp);

                // Rewind the stream...
                ms.Seek(0, SeekOrigin.Begin);

                // Tell the WPF image to use this stream...
                bi.StreamSource = ms;
                bi.EndInit();

                backgroundImage.Source = bi;
            }

        }

        private void doneButton_Click(object sender, RoutedEventArgs e)
        {
            deck.Label = deckLabelTextBox.Text;

            if (randomRadioButton.IsChecked == true)
            {
                DeckSetupDirectorUsingRandomSymbols randomSetupDirector = new DeckSetupDirectorUsingRandomSymbols();
                randomSetupDirector.FromSymbolSet = symbolSet;
                randomSetupDirector.TargetDeck = deck;
                randomSetupDirector.Setup();

                OperationResult result = services.deckManager.Update(session, deck);
            }
            else
            {
                DeckSetupDirectorUsingSelectedSymbols specificSetupDirector = new DeckSetupDirectorUsingSelectedSymbols();
                specificSetupDirector.NumberOfCards = Convert.ToInt32(cardCountTextBox.Text);
                specificSetupDirector.FromSymbolSet = symbolSet;
                specificSetupDirector.TargetDeck = deck;
                specificSetupDirector.Setup();

                OperationResult result = services.deckManager.Update(session, deck);
            }

            this.Close();
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void addImageButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "All files (*.*)|*.*";
            fileDialog.Title = "Select a Back Image";
            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string fileName = fileDialog.FileName;
                System.Drawing.Image backImage= System.Drawing.Image.FromFile(fileName);
                deck.BackImage = backImage;

                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri(fileName, UriKind.Absolute);
                bi.DecodePixelWidth = 200;
                bi.DecodePixelHeight = 200;
                bi.EndInit();

                backgroundImage.Source = bi;
            }
        }

        private void randomRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            cardCountTextBox.Visibility = Visibility.Hidden;
        }

        private void specificRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            cardCountTextBox.Visibility = Visibility.Visible;
        }

       

    }
}
